// 校验身份证号码
function validateIdCard(idCard) {
  const fifteenPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}$/;
  const eighteenPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;
  if (idCard.length === 15) {
    // 15 位身份证号码校验
    return fifteenPattern.test(idCard);
  } else if (idCard.length === 18) {
    // 18 位身份证号码校验
    if (!eighteenPattern.test(idCard)) {
      return false;
    }
    // 校验码验证
    const idCardBase = idCard.slice(0, -1);
    const checkCode = idCard.slice(-1).toUpperCase();
    return checkIdCardCode(idCardBase) === checkCode;
  } else {
    return false;
  }
}

// 计算身份证号码的校验码
function checkIdCardCode(idCardBase) {
  const weightFactors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  const checkCodeMap = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  let sum = 0;
  for (let i = 0; i < idCardBase.length; i++) {
    sum += idCardBase[i] * weightFactors[i];
  }
  return checkCodeMap[sum % 11];
}

export {
	validateIdCard,
}
